<template>
 <div class="page">
 <div class="page__bd">
  <div class="weui-search-bar">
  <div class="weui-search-bar__form">
   <div class="weui-search-bar__box">
   <icon class="weui-icon-search_in-box" type="search" size="14"></icon>
   <input type="text" class="weui-search-bar__input" placeholder="搜索" v-model="inputVal" :focus="inputShowed" @input="inputTyping" />
   <div class="weui-icon-clear" v-if="inputVal.length > 0" @click="clearInput">
    <icon type="clear" size="14"></icon>
   </div>
   </div>
   <label class="weui-search-bar__label" :hidden="inputShowed" @click="showInput">
   <icon class="weui-icon-search" type="search" size="14"></icon>
   <div class="weui-search-bar__text">搜索</div>
   </label>
  </div>
  <div class="weui-search-bar__cancel-btn" :hidden="!inputShowed" @click="searchData">搜索</div>
  </div>
  <!-- <div class="weui-cells searchbar-result" v-if="inputVal.length > 0">
  <navigator url="" class="weui-cell" hover-class="weui-cell_active">
   <div class="weui-cell__bd">
   <div>实时搜索文本</div>
   </div>
  </navigator>
  <navigator url="" class="weui-cell" hover-class="weui-cell_active">
   <div class="weui-cell__bd">
   <div>实时搜索文本</div>
   </div>
  </navigator>
  <navigator url="" class="weui-cell" hover-class="weui-cell_active">
   <div class="weui-cell__bd">
   <div>实时搜索文本</div>
   </div>
  </navigator>
  <navigator url="" class="weui-cell" hover-class="weui-cell_active">
   <div class="weui-cell__bd">
   <div>实时搜索文本</div>
   </div>
  </navigator>
  </div> -->
 </div>
 </div>
</template>

<script>
import {setStorage, getStorage} from '@/utils/wechat'
import constants from '../constants/constants'
export default {
  data() {
    return {
      inputShowed: false,
      inputVal: "",
      tagArr:[]
    };
  },
  mounted(){
    this.getSearchTag();
    this.$root.$eventHub.$on('removeTag',(target) => {
      this.tagArr = []
    });
  },
  methods: {
    showInput() {
      this.inputShowed = true;
    },
    hideInput() {
      this.inputVal = "";
      this.inputShowed = false;
    },
    async getSearchTag(){
      const searchTab = getStorage(constants.STORE_CONSTANTS.KEY_SEARCH_TAG)
      this.tagArr = searchTab || []
    },
    async searchData() {
      if(this.inputVal == ''){
        this.$wx.showShortToast('请输入搜索商品名称或品牌')
        return
      }
      const data = await this.$fly.post(this.$url.URL_PRODUCT_COUNT,{
        tag: this.inputVal
      })
      if (data.successCode === 1) {
        var total = data.data.total;
        if(total <= 0){
          this.$emit('monitorCount',true)
        } else{
          let hasTag = this.tagArr.indexOf(this.inputVal);
          if(hasTag == -1) {
            this.tagArr.push(this.inputVal)
            await setStorage(constants.STORE_CONSTANTS.KEY_SEARCH_TAG, this.tagArr)
          }
          this.$wx.jumper('/pages/productList2/main?word='+ this.inputVal +'&title='+ this.inputVal)
          this.$emit('RefreshSearch')
        }
        this.inputVal = "";
      } else {

      }
    },
    clearInput() {
      this.inputVal = "";
    },
    inputTyping(e) {
      this.inputVal = e.mp.detail.value;
      console.log(this.inputVal);
    }
  }
};
</script>

<style scoped>
.searchbar-result {
  position: absolute; top: 48px; left: 0; right: 0;
  margin-top: 0;
  font-size: 14px;
}
.searchbar-result:before {
  display: none;
}
.weui-cell__bd {
  padding: 6px 12px
}
.weui-search-bar__cancel-btn{color: #000;}
</style>
